#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>

#define DTSBEEP_CNT 1
#define DTSBEEP_NAME "dtsbeep"

#define OFF 0
#define ON  1

static void __iomem *IMX6U_CCM_CCGR1;
static void __iomem *SW_MUX_GPIO1_IO019;
static void __iomem *SW_PAD_GPIO1_IO019;
static void __iomem *GPIO1_DR;
static void __iomem *GPIO1_GDIR;

struct dstbeep_dev{     //设备结构体
    dev_t devid;
    struct cdev cdev;
    struct class *class;
    struct device *device;
    int major;
    int minor;
    struct device_node* nd;  //设备树中节点
};

struct dstbeep_dev dtsbeep; //设备实例

void beep_switch(u8 sta)    //beep转换开关
{
	u32 val = 0;
	if( sta == ON){
		val = readl(GPIO1_DR);
		val|= (1 << 19);		
		writel(val, GPIO1_DR);
	}else if(sta == OFF){
		val = readl(GPIO1_DR);
		val &= ~(1 << 19);
		writel(val, GPIO1_DR);
	}	
}

static int beep_open(struct inode *inode,struct file *filp)
{   
    filp->private_data = &dtsbeep;
    return 0;
}


static ssize_t beep_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
    return 0;
}

static ssize_t beep_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
    int retvalue;
    unsigned char databuf[1];
    unsigned char beepstat;

    retvalue = copy_from_user(databuf,buf,cnt);
    if(retvalue < 0 ){
        printk("kernel write failed!\r\n");
        return -EFAULT;
    }

    beepstat = databuf[0];

    if(beepstat == ON){
        beep_switch(ON);
    }else if(beepstat==OFF){
        beep_switch(OFF);
    }

    return 0;
}

static int beep_release(struct inode *inode, struct file *filp)
{
	return 0;
}

static struct file_operations dtsbeep_fops = {
	.owner = THIS_MODULE,
	.open = beep_open,
	.read = beep_read,
	.write = beep_write,
	.release = 	beep_release,
};

static int __init beep_init(void)
{
    unsigned int val = 0;
    int ret;
    unsigned int regdata[14];
    const char *str;            //
    struct property *proper;    //设备树获取属性

    /* 获取设备树中设备节点 */
    dtsbeep.nd = of_find_node_by_path("/beep");
    if(dtsbeep.nd == NULL){
       printk("beep node nost find!\r\n");
		return -EINVAL; 
    }else{
        printk("beep node find!\r\n");
    }

    /* 2、获取compatible属性内容 */
	proper = of_find_property(dtsbeep.nd, "compatible", NULL);
	if(proper == NULL) {
		printk("compatible property find failed\r\n");
	} else {
		printk("compatible = %s\r\n", (char*)proper->value);
	}

    /* 3、获取status属性内容 */
	ret = of_property_read_string(dtsbeep.nd, "status", &str);
	if(ret < 0){
		printk("status read failed!\r\n");
	} else {
		printk("status = %s\r\n",str);
	}

    /* 4、获取reg属性内容 */
	ret = of_property_read_u32_array(dtsbeep.nd, "reg", regdata, 10);
	if(ret < 0) {
		printk("reg property read failed!\r\n");
	} else {
		u8 i = 0;
		printk("reg data:\r\n");
		for(i = 0; i < 10; i++)
			printk("%#X ", regdata[i]);
		printk("\r\n");
	}
    /*初始化LED寄存器*/
#if 0
    /* 1、寄存器地址映射 */
	IMX6U_CCM_CCGR1 = ioremap(regdata[0], regdata[1]);
	SW_MUX_GPIO1_IO03 = ioremap(regdata[2], regdata[3]);
  	SW_PAD_GPIO1_IO03 = ioremap(regdata[4], regdata[5]);
	GPIO1_DR = ioremap(regdata[6], regdata[7]);
	GPIO1_GDIR = ioremap(regdata[8], regdata[9]);
#else
    IMX6U_CCM_CCGR1 = of_iomap(dtsbeep.nd, 0);
	SW_MUX_GPIO1_IO019 = of_iomap(dtsbeep.nd, 1);
  	SW_PAD_GPIO1_IO019 = of_iomap(dtsbeep.nd, 2);
	GPIO1_DR = of_iomap(dtsbeep.nd, 3);
	GPIO1_GDIR = of_iomap(dtsbeep.nd, 4);
#endif

    // val = readl(IMX6U_CCM_CCGR1);
	// val &= ~(3 << 26);				/* 清除以前的设置 */
	// val |= (3 << 26);				/* 设置新值 */
	// writel(val, IMX6U_CCM_CCGR1);

	// /* 设置GPIO1_IO03复用功能，将其复用为GPIO1_IO019 */
	// writel(5, SW_MUX_GPIO1_IO019);
	// writel(0x10B0, SW_PAD_GPIO1_IO019);

	// /* 设置GPIO1_IO019为输出功能 */
	// val = readl(GPIO1_GDIR);
	// val &= ~(1 << 19);			/* 清除以前的设置 */
	// val |= (1 << 19);			/* 设置为输出 */
	// writel(val, GPIO1_GDIR);

	// /* 默认关闭Beep */
	// val = readl(GPIO1_DR);
	// val &= ~(1 << 19);	
	// writel(val, GPIO1_DR);
	val = readl(IMX6U_CCM_CCGR1);
	val &= ~(3 << 26);				/* 清除以前的设置 */
	val |= (3 << 26);				/* 设置新值 */
	writel(val, IMX6U_CCM_CCGR1);

	/* 设置GPIO1_IO03复用功能，将其复用为GPIO1_IO019 */
	writel(5, SW_MUX_GPIO1_IO019);
	writel(0x10B0, SW_PAD_GPIO1_IO019);

	/* 设置GPIO1_IO019为输出功能 */
	val = readl(GPIO1_GDIR);
	val &= ~(1 << 19);			/* 清除以前的设置 */
	val |= (1 << 19);			/* 设置为输出 */
	writel(val, GPIO1_GDIR);

	/* 默认关闭Beep */
	val = readl(GPIO1_DR);
	val &= ~(1 << 19);	
	writel(val, GPIO1_DR);
	

/* 注册字符设备驱动 */
	/* 1、创建设备号 */
	if (dtsbeep.major) {		/*  定义了设备号 */
		dtsbeep.devid = MKDEV(dtsbeep.major, 0);
		register_chrdev_region(dtsbeep.devid, DTSBEEP_CNT, DTSBEEP_NAME);
	} else {						/* 没有定义设备号 */
		alloc_chrdev_region(&dtsbeep.devid, 0, DTSBEEP_CNT, DTSBEEP_NAME);	/* 申请设备号 */
		dtsbeep.major = MAJOR(dtsbeep.devid);	/* 获取分配号的主设备号 */
		dtsbeep.minor = MINOR(dtsbeep.devid);	/* 获取分配号的次设备号 */
	}
	printk("dtsbeep major=%d,minor=%d\r\n",dtsbeep.major, dtsbeep.minor);	
	
	/* 2、初始化cdev */
	dtsbeep.cdev.owner = THIS_MODULE;
	cdev_init(&dtsbeep.cdev, &dtsbeep_fops);
	
	/* 3、添加一个cdev */
	cdev_add(&dtsbeep.cdev, dtsbeep.devid, DTSBEEP_CNT);

	/* 4、创建类 */
	dtsbeep.class = class_create(THIS_MODULE, DTSBEEP_NAME);
	if (IS_ERR(dtsbeep.class)) {
		return PTR_ERR(dtsbeep.class);
	}

	/* 5、创建设备 */
	dtsbeep.device = device_create(dtsbeep.class, NULL, dtsbeep.devid, NULL, DTSBEEP_NAME);
	if (IS_ERR(dtsbeep.device)) {
		return PTR_ERR(dtsbeep.device);
	}
	
	return 0;
}

static void __exit beep_exit(void){
    /* 取消映射 */
	iounmap(IMX6U_CCM_CCGR1);
	iounmap(SW_MUX_GPIO1_IO019);
	iounmap(SW_PAD_GPIO1_IO019);
	iounmap(GPIO1_DR);
	iounmap(GPIO1_GDIR);

	/* 注销字符设备驱动 */
	cdev_del(&dtsbeep.cdev);/*  删除cdev */
	unregister_chrdev_region(dtsbeep.devid, DTSBEEP_CNT); /* 注销设备号 */

	device_destroy(dtsbeep.class, dtsbeep.devid);
	class_destroy(dtsbeep.class);
}


module_init(beep_init);
module_exit(beep_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zzlyang");